<?php


namespace app\admin\controller\tickets;


use app\common\controller\Adminbase;
use think\Db;

class History extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where=array();
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['member_id'])) {
                    $where[] = ['m.id', '=', $filter['member_id']];
                }
                if (isset($filter['type'])) {
                    $where[] = ['th.type', '=', $filter['type']];
                }
            }
            $_list = Db::name('member_ticket_history')
                ->alias('th')
                ->field('th.*,m.nickname as username,tc.name as tcname')
                ->join('member m','th.member_id=m.id')
                ->join('ticket_category tc','th.ticket_cate=tc.id')
                ->where($where)
                ->page($page, $limit)
                ->order('th.create_time desc')
                ->select();
            foreach ($_list as $k=>$v){
                $_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            }

            $total = Db::name('member_ticket_history')
                ->alias('th')
                ->field('th.*,m.name as username,tc.name as tcname')
                ->join('member m','th.member_id=m.id')
                ->join('ticket_category tc','th.ticket_cate=tc.id')
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch('tickets/history/index');
    }
}